package com.javalong.affairs.base.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.javalong.affairs.R;
import com.javalong.affairs.base.utils.DisplayUtils;

public class MainBlockButtons extends FrameLayout implements OnClickListener {
	private final static int HORIZONTAL_SPACING = 60;
	private final static int VERTICAL_SPACING = 10;
	private final static int VERTICAL_SPACING_SMALL = 5;
	private final static int MARGIN_TOP = 20;
	private float btnWidth;
	private float btnHeigth;
	private FrameLayout ll_root;
	int[] drawables = new int[] { R.drawable.a1, R.drawable.a2, R.drawable.a3,
			R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7,
			R.drawable.a8, R.drawable.a9 };

	public MainBlockButtons(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public MainBlockButtons(Context context) {
		super(context);
		init();
	}

	private void init() {
		ll_root = new FrameLayout(getContext());
		addView(ll_root);
		/*
		 * btnWidth = (DisplayUtils.getOutMetrics().widthPixels - 3
		 * HORIZONTAL_SPACING)/2; btnHeigth = (btnWidth * 378 / 444 -
		 * VERTICAL_SPACING_SMALL) / 2;
		 */
		int allHeight = DisplayUtils.getOutMetrics().heightPixels-DisplayUtils.dip2px(getContext(), 160+100);
		btnHeigth =(float) (((allHeight-2.0*VERTICAL_SPACING)/3-VERTICAL_SPACING_SMALL)/2);

		btnWidth = (btnHeigth * 2 + VERTICAL_SPACING_SMALL) * 444 / 378;

		for (int i = 0; i < drawables.length; i++) {
			if (i >= 6) {
				createBigButton(i);
			} else {
				createSmallButton(i);
			}
		}
	}

	private void createBigButton(int i) {
		ImageView button = new ImageView(getContext());
		button.setScaleType(ScaleType.FIT_XY);
		FrameLayout.LayoutParams ll_param = new FrameLayout.LayoutParams(
				(int) btnWidth, (int) (btnHeigth * 2 + VERTICAL_SPACING_SMALL));
		ll_param.leftMargin = (int) ((i - 5) % 2
				* (HORIZONTAL_SPACING + btnWidth) );
		ll_param.topMargin = (int) ((i - 6)
				* (VERTICAL_SPACING + 2 * btnHeigth + VERTICAL_SPACING_SMALL) + MARGIN_TOP);
		button.setImageResource(drawables[i]);
		button.setOnClickListener(this);
		button.setTag(i);
		ll_root.addView(button, ll_param);
	}

	private void createSmallButton(int i) {
		ImageView button = new ImageView(getContext());
		button.setScaleType(ScaleType.FIT_XY);
		FrameLayout.LayoutParams ll_param = new FrameLayout.LayoutParams(
				(int) btnWidth, (int) (btnHeigth));
		ll_param.leftMargin = (int) (i / 2 % 2
				* (HORIZONTAL_SPACING + btnWidth) );
		ll_param.topMargin = (int) (i / 2
				* (VERTICAL_SPACING + 2 * btnHeigth + VERTICAL_SPACING_SMALL)
				+ MARGIN_TOP + i % 2 * (btnHeigth + VERTICAL_SPACING_SMALL));
		button.setImageResource(drawables[i]);
		button.setOnClickListener(this);
		button.setTag(i);
		ll_root.addView(button, ll_param);
	}

	public interface OnButtonClickListener {
		void onButtonClick(int index);
	}

	private OnButtonClickListener onButtonClickListener;

	public void setOnButtonClickListener(
			OnButtonClickListener onButtonClickListener) {
		this.onButtonClickListener = onButtonClickListener;
	}

	@Override
	public void onClick(View v) {
		if (onButtonClickListener != null) {
			onButtonClickListener.onButtonClick((Integer) v.getTag());
		}
	}
}
